package com.liuhengjia.config;

import cn.itcast.pojo.Country;
import cn.itcast.pojo.Province;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommonConfig {

    /**
     * Bean 注解注入 Country 对象
     *
     * @return cn.itcast.pojo.Country
     */
    @Bean
    @ConditionalOnProperty(prefix = "country", name = {"name", "system"})
    public Country getCountry(@Value("${country.name}") String name,
                              @Value("${country.system}") String system) {
        Country china = new Country();
        china.setName(name);
        china.setSystem(system);
        return china;
    }

    /**
     * Bean 注解注入 Province 对象
     *
     * @return cn.itcast.pojo.Province
     */
//    @Bean
    @Bean("province")
//    @ConditionalOnBean(Country.class)
    @ConditionalOnClass(name = "org.springframework.web.servlet.DispatcherServlet")
    public Province getProvince(Country country) {
        System.out.println("province::country = " + country);
        return new Province();
    }
}
